/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package thinkinginjava.svn.io.text;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author public_enemy
 */
public class TextFile extends ArrayList<String> {

    /**
     * Read a file as a single string
     * @param fileName
     * @return
     */
    public static String read(String fileName) {
        StringBuilder sb = new StringBuilder();
        try {
            BufferedReader in = new BufferedReader(new FileReader(new File(fileName).getAbsoluteFile()));
            try {
                String s;
                while ((s = in.readLine()) != null) {
                   // System.out.println(s);
                    sb.append(s);
                    sb.append("\n");
                }
            } catch (IOException ex) {
                Logger.getLogger(TextFile.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    in.close();
                } catch (IOException ex) {
                    Logger.getLogger(TextFile.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } catch (FileNotFoundException fnfe) {
            Logger.getLogger(TextFile.class.getName()).log(Level.SEVERE, null, fnfe);
        }
        return sb.toString();
    }

    /**
     * Write a single file in one method call:
     * @param fileName
     * @param text
     */
    public static void write(String fileName, String text) {

        try {
            PrintWriter out = new PrintWriter(new File(fileName).getAbsoluteFile());
            try {
                out.print(text);
            } finally {
                out.close();
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(TextFile.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Read a file, split by a regular expression:
     * @param filename
     * @param splitter
     */
    public TextFile(String filename, String splitter) {
        super(Arrays.asList(read(filename).split(splitter)));
        //Regular expression split() ofter leaves an empty 
        //String at the first possition
        if (get(0).equals("")) {
            remove(0);
        }
    }

    /**
     * Normally read by lines:
     * @param fileName
     */
    public TextFile(String fileName) {
        this(fileName, "\n");
    }

    /**
     * 
     * @param fileName
     */
    public void write(String fileName) {
        try {
            PrintWriter out = new PrintWriter(
                    new File(fileName).getAbsoluteFile());
            try {
                for (String item : this) {
                    out.println(item);
                }
            } finally {
                out.close();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Simple Test
     * @param args
     */
    public static void main(String[] args) {
        String file = read("TextFile.java");
        write("text2.txt", file);
    }
}
